local test = UnitTest:New("02DongleCore")
local Dongle = DongleStub("Dongle-1.0")

function test:dongle_new()
	assert_fails(function() Dongle:New(1) end, argerror(2))
	assert_fails(function() Dongle:New(nil, 1) end, argerror(2))
	local obj,name = Dongle:New("Alpha")
	assert(type(obj) == "table")
	assert(name == "Alpha")

	assert_fails(function() Dongle:New("Alpha") end, "A Dongle with the name 'Alpha' is already registered.")

	local tbl = {}
	local obj,name = Dongle:New("Beta",tbl)
	assert(obj == tbl)
	assert(name == "Beta")
end

function test:dongle_init_load_order()
	local zeta = Dongle:New("Zeta")
	local eta = Dongle:New("Eta")

	local zetaalpha = zeta:NewModule("Zeta-Alpha")
	local etaalpha = eta:NewModule("Eta-Alpha")

	local order = {}
	local function init(self)
		table.insert(order, self)
	end

	zeta.Initialize = init
	eta.Initialize = init
	zetaalpha.Initialize = init
	etaalpha.Initialize = init

	local OnEvent = Dongle.frame:GetScript("OnEvent")
	
	OnEvent(Dongle.frame, "ADDON_LOADED")
	assert(order[1] == zeta)
	assert(order[2] == eta)
	assert(order[3] == zetaalpha)
	assert(order[4] == etaalpha)
end

function test:dongle_enable_load_order()
	local theta = Dongle:New("Theta")
	local iota = Dongle:New("Iota")

	local thetaalpha= theta:NewModule("ThetaAlpha")
	local iotaalpha = iota:NewModule("IotaAlpha")

	local order = {}
	local function enable(self)
		table.insert(order, self)
	end

	theta.Enable = enable
	iota.Enable = enable
	thetaalpha.Enable = enable
	iotaalpha.Enable = enable

	local OnEvent = Dongle.frame:GetScript("OnEvent")
	
	OnEvent(Dongle.frame, "ADDON_LOADED")
	OnEvent(Dongle.frame, "PLAYER_LOGIN")
	assert(order[1] == theta)
	assert(order[2] == iota)
	assert(order[3] == thetaalpha)
	assert(order[4] == iotaalpha)
end

function test:dongle_special_events()
	local j = Dongle:New("special_events")
	local err = "You cannot register for the '%s' event.  Use the '%s' method instead."

	assert_fails(function() j:RegisterEvent("PLAYER_LOGIN") end, 
		string.format(err, "PLAYER_LOGIN", "Enable"))
	assert_fails(function() j:RegisterEvent("PLAYER_LOGOUT") end,
		string.format(err, "PLAYER_LOGOUT", "Disable"))
end

test:Run()